using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Util
{
	public class MemoryStreamEx : StreamEx
	{
		protected MemoryStreamEx()
		{
		}

		public MemoryStreamEx(IEnumerable<char> data)
		{
			_data = new List<char>(data);
		}

		protected List<char> _data;

		protected int _position = 0;

		public override int Position
		{
			[DebuggerStepThrough]
			get { return _position; }
			[DebuggerStepThrough]
			set { _position = value; }
		}

		public override char Read()
		{
			if (Depleted)
				throw new EndOfStreamException();
			return _data[_position++];
		}

		public override void Write(char ch)
		{
			if (!Depleted)
				_data[_position++] = ch;
			else
			{
				_data.Add(ch);
				_position++;
			}
		}

		public override void Insert(char ch)
		{
			if (Depleted)
			{
				Write(ch);
			}
			else
			{
				_data.Insert(_position, ch);
			}
		}

		public override void Delete(int count)
		{
			if (!Depleted)
				_data.RemoveRange(_position, count);
		}

		public override int Length
		{
			[DebuggerStepThrough]
			get { return _data.Count; }
		}

		public override void Dispose()
		{
		}
	}
}
